<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Game_model extends CI_Model {

    function joined_game($game_id, $user_id, $killcode)
    {
         /*check to see if game is active/joinable/paused/non joinable/human win/zombie win
        find number of players AND humans and increase them by 1
         update the game_id for the user_id given with the game_id given
         update killcode in current_game with the killcode given
         set timed_tagged, starve_time, fed, and tags to 0
         set status id to human*/
        $records = $this->db->get_where("games",array('id'=> $game_id));
            foreach ($records->result() as $row) {
                $status_id = $row->Game_Status_id;
                $players = $row->players;
                $humans = $row->humans;
            }
            $current_game = $this->db->get_where('current_game',array('User_id'=>$user_id));
            foreach($current_game as $row){
                $time_tagged = $row->time_tagged;
                $time_alive = $row->time_alive;
                $starve_time = $row->starve_time;
                $fed = $row->fed;
                $tags = $row->tags;
                $is_human = $row->status_id;
            }
            if($status_id == 1){
                $humans++;
                $players++;
                $data = array(
                    'humans' => $humans,
                    'players' => $players,
                    'killcode' => $killcode,
                    'time_tagged' => '0000-00-00 00:00:00',
                    'time_alive' => '0000-00-00 00:00:00',
                    'starve_time' => '0000-00-00 00:00:00',
                    'fed' => 0,
                    'tags' => 0
                );
                $this->db->where('User_id',$user_id);
                $this->db->update('current_game',$data);
                
                /*
                    Update leaderboards.
                */
                $leaderboard = $this->db->get_where("leaderboard",array('User_id'=> $user_id));
                foreach($leaderboard as $new_lb){
                    $survived = $new_lb->survived;
                    $games_played = $new_lb->games_played;
                    $time_alive_old = $new_lb->time_alive;
                    $fed_old = $new_lb->fed;
                    $tags_old = $new_lb->tags;
                }
                $e = new DateTime('0000-00-00 00:00:00');
                $f = clone $e;
                $e->add($time_alive);
                $e->add($time_alive_old);
                $time_alive_new = $f->diff($e)->format("Y-m-d G:i:s");
                $games_played++;
                $tags_new = $tags + $tags_old;
                $fed_new = $fed + $fed_old;
                if($is_human == 1)
                    $survived++;
                
                $new_leaderboard = array(
                    'tags' => $tags_new,
                    'fed' => $fed_new,
                    'survived' => $survived,
                    'games_played' => $games_played,
                    'time_alive' => $time_alive_new
                );
                $this->db->where('User_id',$user_id);
                $this->db->update('leaderboard',$new_leaderboard);
            }
            else
                return false;
    }
}    